package com.songshuai.sales.client.service.impl;

import com.songshuai.sales.client.service.WxPlatformService;
import com.songshuai.sales.client.dao.WxPlatformMapper;

import com.songshuai.webapi.baseapi.model.sales.WxPlatform;
import com.songshuai.webapi.common.redis.RedisUtil;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


/**
 * 此类为[基础代码]自动生成,已经拥有基本的增删改成操作 
 * <其他请自行扩展>
 * @Author: 王冰兰
 * @Date: 2019-03-14 14:37:37
 * 
 */
@Service
public class WxPlatformServiceImpl implements WxPlatformService{
 
      
    @Autowired
	private WxPlatformMapper wxPlatformMapper;

    @Autowired
    private RedisUtil redisUtil;

    //根据appId获取accessToken
    public String getAccessToken(String appId){
        String accessToken = redisUtil.getString(appId);
        if(accessToken == null){
            //缓存不存在
            //通过数据库查询
            WxPlatform wxPlatform = new WxPlatform();
            wxPlatform.setAppId(appId);
            WxPlatform result = selectByEntity(wxPlatform);
            if(result != null) {//数据库存在时
                accessToken = result.getAccessToken();
                redisUtil.set(appId,accessToken,result.getExpiresIn());
            }
        }
        return accessToken;
    }
	
	
	/**
     * 新增
     * @param entity 
     */
    public int insertSelective (WxPlatform entity) { 
        return this.wxPlatformMapper.insertSelective(entity);
    }	
    
    /**
     * 批量新增
     * @param list 
     */
    public void insertBatch(List<WxPlatform> list) {       
        this.wxPlatformMapper.insertBatch(list); 
    }
    
    /**
     * 根据主键修改单条数据
     * @param entity 
     */
    public int updateByPrimaryKey(WxPlatform entity) {
        return this.wxPlatformMapper.updateByPrimaryKey(entity);
    }
    
    /**
     * 根据主键批量修改数据
     * @param list 
     */
    public void updateBatch(List<WxPlatform> list) {      
        this.wxPlatformMapper.updateBatch(list);     
    }
    
    /**
     * 根据主键查询单条数据
     * @param id
     */
    public WxPlatform selectByPrimaryKey(Integer id) {
        return this.wxPlatformMapper.selectByPrimaryKey(id);
    }
    
    /**
     * 根据条件查询单条记录
     * @param entity 
     */
    public WxPlatform selectByEntity(WxPlatform entity) {
        return this.wxPlatformMapper.selectByEntity(entity);
    }
      
    /**
     * 根据条件查询多条记录
     * @param entity 
     */
    public List<WxPlatform> selectByEntityList(WxPlatform entity) {
        return this.wxPlatformMapper.selectByEntityList(entity);
    }
    
    /**
     * 根据条件查询Id
     * @param entity 
     */
    public Integer selectById(WxPlatform entity) {
        return this.wxPlatformMapper.selectById(entity);
    }
    
    /**
     * 根据条件查询Ids
     * @param entity 
     */
    public List<Integer> selectByIds(WxPlatform entity) {
        return this.wxPlatformMapper.selectByIds(entity);
    }
    
    /**
     * 根据主键删除单条记录
     * @param id
     */
    public void deleteByPrimaryKey(Integer id) {
        this.wxPlatformMapper.deleteByPrimaryKey(id);
    }
    
    /**
     * 根据主键删除多条记录
     * @param list
     */
    public void deleteBatch(List<Integer> list) {
        this.wxPlatformMapper.deleteBatch(list);
    }
    
    /**
     * 根据某些条件删除
     * @param entity
     */
    public void deleteByEntity(WxPlatform entity) {
        this.wxPlatformMapper.deleteByEntity(entity);
    }
    
   
}